export default {
  namespaced: true,

  state: () => ({
    // { goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state }
    cart: JSON.parse(uni.getStorageSync('cart') || '[]'),
  }),

  mutations: {
	  addToCart(state, item) {
		const good = state.cart.find(x => {
			return x.goods_id === item.goods_id
		})
		if (good) {
			good.goods_count++
		} else {
			state.cart.push(item)
		}
		this.commit('m_cart/saveToStorage')
	  },
	  saveToStorage(state) {
	     uni.setStorageSync('cart', JSON.stringify(state.cart))
	  }
  },

  getters: {
	  total(state) {
		  let sum = 0
		  state.cart.forEach(goods => {
			  sum += goods.goods_count
		  })
		  return sum
	  }
  },
}